﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DemoSearchInGridBusiness;

public partial class DemoBoundField : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        DataTable dtIntermediate = GetGridDataSource();
        List<EmployeeInformation> lstToEmployees = GetAllEmployees(dtIntermediate);
        grdBoundField.DataSource = lstToEmployees;
        grdBoundField.DataBind();
        ctrlSearch.UpdateOriginalData();
    }

    private List<EmployeeInformation> GetAllEmployees(DataTable dtIntermediate)
    {
        List<EmployeeInformation> employeesList = new List<EmployeeInformation>();
        foreach (DataRow row in dtIntermediate.Rows)
        {
            EmployeeInformation employeeInformation = new EmployeeInformation();
            employeeInformation.ID = (int)row["EmployeeID"];
            employeeInformation.FirstName = row["FirstName"].ToString();
            employeeInformation.LastName = row["LastName"].ToString();
            employeeInformation.ContactNo = (Int64)row["ContactNo"];
            employeeInformation.Address = row["Address"].ToString();
            employeesList.Add(employeeInformation);

        }
        return employeesList;
    }

    private DataTable GetGridDataSource()
    {
        SqlConnection conn = new SqlConnection("Data Source=(local);User ID=sa;Password=sa123;Initial Catalog=DemoSearchInGrid;");
        SqlDataAdapter daEmployees = new SqlDataAdapter("select * from Employees", conn);
        DataSet dsEmployees = new DataSet();
        daEmployees.Fill(dsEmployees);
        DataTable dtEmployee = dsEmployees.Tables[0];
        return dtEmployee;
    }
}
